﻿using ezCloud.Api.Common;
using ezCloud.Api.Services;
using ezCloud.Data;
using Microsoft.AspNet.Identity;
using Nest;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlTypes;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;

namespace ezCloud.Api.Controllers
{
    [ApiAuthorize(Roles = "ROLE_HOTEL_OWNER")]
    [RoutePrefix("api/BackendPayment")]
    public class HotelBackendPaymentController : ezController
    {
        public HotelBackendPaymentController()
        {

        }

        public class BackendPaymentModel
        {
            public decimal Amount { get; set; }
            public decimal additionalAmount { get; set; }
            public string Description { get; set; }
            public bool isDomestic { get; set; }
            public string vpc_Locale { get; set; }
            public string ReferralCode { get; set; }
        }

        [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("CreateBackendPayment")]
        public IHttpActionResult CreateBackendPayment(BackendPaymentModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            OnePayPayment onePay = new OnePayPayment();
            return Ok(onePay.createPayment(model.Amount, model.additionalAmount, model.Description, model.isDomestic, model.vpc_Locale, model.ReferralCode));
        }
        [HttpGet]
        [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("ProcessReturnUrl")]
        public IHttpActionResult ProcessReturnUrl()
        {
            var onePay = new OnePayPayment();
            var paymentReturn = onePay.ProcessReturnUrl(false);
            //paymentReturn.tx
            var isDomestic = paymentReturn.isDomestic == "true" ? "DOMESTIC_ERROR_" : "INTERNATIONAL_ERROR_";
            var result = new
            {
                status = paymentReturn.status,
                statusCode= isDomestic + paymentReturn.txnResponseCode
            };

            // Referral Program
            if (paymentReturn.status == paymentResult.OK)
            {
                var user = Common.UserUtitily.getUserProfile();
                var currentHotel = context.Hotels.Where(htl => htl.HotelId == user.HotelId).FirstOrDefault();
                if (currentHotel != null)
                {
                    var referralProgram = context.EzReferralPrograms.FirstOrDefault();
                    if (referralProgram != null)
                    {
                        if (referralProgram.Status == true && ReferralProgramUltility.CheckIfHotelFirstPayment(currentHotel.HotelId) == true)
                        {
                            var ezReferralCode = new EzReferralCodes();
                            ezReferralCode.CreatedDate = DateTime.UtcNow;
                            ezReferralCode.EzReferralProgramId = referralProgram.EzReferralProgramId;
                            ezReferralCode.HotelId = currentHotel.HotelId;
                            ezReferralCode.Code = ReferralProgramUltility.ReferralCodeGenerator(currentHotel.HotelId);
                            context.EzReferralCodes.InsertOnSubmit(ezReferralCode);

                            var ezReferralProgramLog = new EzReferralProgramLogs();



                            var commission_A = referralProgram.CommisionRateA;
                            var commission_B = referralProgram.CommisionRateB;

                        }
                    }
                }
            }

            return Ok(result);
        }

        [HttpPost]
        [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("ProcessReturnUrlIPN")]
        public IHttpActionResult ProcessReturnUrlIPN()
        {
            var onePay = new OnePayPayment();
            var paymentReturn = onePay.ProcessReturnUrl(true);

            return Ok(paymentReturn.status);
        }

        
    }
}